/* $Id:background_level.cc $ */
/*
   Copyright (C) 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>
   Part of the Moros Project http://moros.sourceforge.net/

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.

   See the COPYING file for more details.
*/

#define DEBUG_ROUTINE "background_level"

#include <sstream>
#include <fstream>
#include "../general/util.h"
#include "../general/log.h"
#include "../general/channel.h"
#include "background_level.h"
#include "background.h"
#include "../general/general.h"

background_level::background_level() {
    background=0;
    level=0;
}

// Field : background

class background& background_level::getBackground() const { if (background==0) throw nullpointer(); return *background; }
void background_level::setBackground(class background &v) { background=&v; }

// Field : level

const int background_level::getLevel() const { return level; }
void background_level::setLevel(const int v) { level=v; }
int background_level::operator<(const class background_level &ck) const {
    return (getBackground().getNr() < ck.getBackground().getNr())?1:0;
}

void background_level::save(class channel &ch) {
    ch.write("background_level", "");
    ch.write("b", getBackground().getName());
    ch.write("l", getLevel());
}

void background_level::load(class channel &ch) {
    if (ch.key=="b" || ch.key=="background") {
        if (!background_repo.isBackground(ch.value)) {
            dbg_state("link unknown background %s", ch.value.c_str());
            class background rec;
            rec.setName(ch.value);
            background_repo.setBackground(rec);
            rec.setNr(0);
        }
        setBackground(background_repo.getBackground(ch.value));
        ch.next();
    }
    if (ch.key=="l" || ch.key=="level") { setLevel(atoi(ch.value.c_str())); ch.next(); }
}

void background_level::xml(ofstream &f, int l, int r) {
    f << ncpy(l, "  ") << "<background_level level=\"" << getLevel() <<
    "\">" << endl;
    if (r<2) try {
        getBackground().xml(f,l+1,r+1);
    } catch (nullpointer n) {}
    f << ncpy(l, "  ") << "</background_level>" << endl;
}
